/*
package com.tfjybj.itoo.exam.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

*/
/**
 * @author 商俊帅
 * @date 2022/01/06 22:58
 **//*

@Configuration
@Data
@ConfigurationProperties(prefix = "spring.redis")
public class RedissonConfig {

    //表示单机
    private String host;

    private String port;

    private String password;

    private String dataBase;


    @Autowired
    private Environment env;

    @Bean
    public RedissonClient getRedisson(){
        Config config = new Config();
        config=buildSingleConfig(config);
        //config=buildSentinelConfig(config);
        //config=buildClusterConfig(config);
        return Redisson.create(config);
    }

    //构建单机版
    private Config buildSingleConfig(Config config){
        if(StringUtils.hasText(host)){
            port=port==null?"6379":port;
            SingleServerConfig singleServerConfig = config.useSingleServer()
                    .setAddress("redis://" + host + ":" + port)
                    .setTimeout(10000)
                    .setRetryAttempts(3)
                    .setRetryInterval(1000)
                    .setPingConnectionInterval(100)
                    .setConnectionMinimumIdleSize(10);
            if(StringUtils.hasText(password)){
                singleServerConfig.setPassword(password);
            }
            if(StringUtils.hasText(dataBase)){
                singleServerConfig.setDatabase(Integer.valueOf(dataBase));
            }
        }

        return config;
    }

    //构建哨兵版
    private Config buildSentinelConfig(Config config){
        String sentinelMaster = env.getProperty("spring.redis.sentinel.master");
        String sentinelNodes = env.getProperty("spring.redis.sentinel.nodes");
        if(StringUtils.hasText(sentinelMaster) & StringUtils.hasText(sentinelNodes)){
            String[] nodeArray = getNodeArray(sentinelNodes);
            SentinelServersConfig sentinelServersConfig = config.useSentinelServers().setMasterName(sentinelMaster);
            for(String s:nodeArray){
                sentinelServersConfig.addSentinelAddress(s);
            }
            if(StringUtils.hasText(password)){
                sentinelServersConfig.setPassword(password);
            }
            if(StringUtils.hasText(dataBase)){
                sentinelServersConfig.setDatabase(Integer.valueOf(dataBase));
            }
        }
        return config;
    }

    //构建集群版
    private  Config buildClusterConfig(Config config){
        String clusterNodes = env.getProperty("spring.redis.cluster.nodes");
        if(StringUtils.hasText(clusterNodes)){
            String[] nodeArray = getNodeArray(clusterNodes);
            ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(nodeArray);
            if(StringUtils.hasText(password)){
                clusterServersConfig.setPassword(password);
            }
        }
        return config;
    }

    //从nodes字符串中截取ip:port,构建一个node数组。
    //主要用于哨兵和集群节点
    private String[] getNodeArray(String s){
        String[] nodeArray;
        if(s.contains(",")){
            nodeArray = s.split(",");
            for(int i=0;i<nodeArray.length;i++){
                nodeArray[i]="redis://"+nodeArray[i];
            }
        }else{
            nodeArray=new String[1];
            nodeArray[0]="redis://"+s;
        }
        return nodeArray;
    }

}
*/
